var liveViewTimeStamp = 1;
var GameID = $('input[name="gameID"]').val();
var xhrLiveView;
var isxhrLiveViewActive = false;


function setUpDeliveryInfoTicker()
{


    $('#deliveryInfo').innerfade({
            animationtype: 'slide',
            speed: 750,
            timeout: 1500,
            type: 'sequence',
            containerheight: '1em'
    });
			
}

function waitForLiveView()
{
  location.hash = "livefeed";
 isxhrLiveViewActive = true;
    var serverURL = '/dazzler/longpoller/';
     if (mainStatsObj.Innings == null)
        {
           currentInnings=1;
        }
        else
        {
           currentInnings = mainStatsObj.Innings;
        }
 var dataToPost= {'pollType':'liveview','GameID': GameID ,'liveViewTimeStamp':liveViewTimeStamp,'innings':currentInnings};

  xhrLiveView = $.ajax({
       type: 'post',
       url: serverURL,
       data: dataToPost,
       dataType: "json",
       async: true,
       cache: false ,
       beforesend: function() {


  },
       success: function(data)
       {
        isxhrLiveViewActive = true;
        $("#liveViewDisplayContainer").hide();

     setUpBallTrajectoryData(data.ballTrajectoryType, data.ClickX, data.ClickY, data.ClickDrag);
    setUpLiveViewPanel(data.lastBallCommentary);
         liveViewTimeStamp = data.newLiveViewTimeStamp;
         waitForLiveView();


       },
            error: function(XMLHttpRequest, textStatus, errorThrown){

                $("#pageheader").html("Live View Long Poller has been switched off.");
                isxhrLiveViewActive = false;

            }

    });

}

function setUpLiveViewPanel(liveViewInfo)
{
 $("#liveViewDisplayContainer").html(liveViewInfo);
 $('#liveViewDisplayContainer').show('blind',{},1000);
 setUpDeliveryInfoTicker();
}

var ClickX = [];
var ClickY = [];
var ClickDrag = [];
var ballTrajectoryType = null;
function setUpBallTrajectoryData(ballTrajectoryTypeArg, clickXPos, clickYPos, clickDragPos)
{
    ballTrajectoryType = ballTrajectoryTypeArg;
    ClickX = [];
    ClickY = [];
    ClickDrag = [];

    $.each(clickXPos, function(increment, ClickXPosValue)
    {
        ClickX.push(parseFloat(ClickXPosValue));
    });


    $.each(clickYPos, function(increment, ClickYPosValue)
    {
        ClickY.push(parseFloat(ClickYPosValue));
    });

    $.each(clickDragPos, function(increment, clickDragPosValue)
    {
        ClickDrag.push(clickDragPosValue);
    });
    drawBallTrajectory();

}